﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Globalization;
using System.Threading;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Calendar;
using System.Diagnostics;

namespace blackhouse.BlackApart.Web.WebService {
    /// <summary>
    /// Summary description for CalendarHandler
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class CalendarHandler : System.Web.Services.WebService {

        #region Web Methods

        [WebMethod]
        public CalendarMonth[] GetMonths_1(int monthsCount) {
            return GetMonths(monthsCount, "", "");
        }

        [WebMethod]
        public CalendarMonth[] GetMonths_2(string language) {
            return GetMonths(0, "", language);
        }

        [WebMethod]
        public CalendarMonth[] GetMonths_3(string dateFormat) {
            return GetMonths(0, dateFormat, "");
        }

        [WebMethod]
        public CalendarMonth[] GetMonths_7(int monthsCount, string dateFormat, string language) {
            return GetMonths(monthsCount, dateFormat, language);
        }

        [WebMethod]
        public CalendarMonth[] GetMonths_4(int monthsCount, string dateFormat) {
            return GetMonths(monthsCount, dateFormat, "");
        }

        [WebMethod]
        public CalendarMonth[] GetMonths_5(int monthsCount, string language) {
            return GetMonths(monthsCount, "", language);
        }

        [WebMethod]
        public CalendarMonth[] GetMonths_6(string dateFormat, string language) {
            return GetMonths(0, dateFormat, language);
        }

        [WebMethod]
        public CalendarMonth[] GetMonths() {
            return GetMonths(0, "", "");
        }

        [WebMethod]
        public DisplayCalendar GetDays(int month, int year) {
            return new DisplayCalendar(month, year);
        }

        [WebMethod]
        public DisplayCalendar GetDaysByGroup(int month, int year, int groupId) {
#if DEBUG
            Stopwatch watch = new Stopwatch();
            watch.Start();
#endif
            DateTime startDate = DisplayCalendar.GetStartDate(month, year);
            DateTime endDate = DisplayCalendar.GetEndDate(startDate);
            ApartmentTypeCalendar calendar = new ApartmentTypeCalendar(groupId, startDate, endDate);
            DisplayCalendar displayCalendar = new DisplayCalendar(month, year);
            displayCalendar.ApplyCalendar(calendar, groupId);
#if DEBUG
            watch.Stop();
            Debug.Write("GetDaysByGroup: " + watch.ElapsedMilliseconds + "ms");
#endif
            return displayCalendar;
        }

        [WebMethod]
        public DisplayCalendar GetDaysByLocale(int month, int year, string language) {
            CultureInfo culture = CultureInfo.GetCultureInfo(language);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
            return new DisplayCalendar(month, year);
        }

        #endregion

        #region Methods

        public CalendarMonth[] GetMonths(int monthsCount, string dateFormat, string language) {

            if (monthsCount == 0)
                monthsCount = 6;
            if (string.IsNullOrEmpty(dateFormat))
                dateFormat = "MMMM";
            if (string.IsNullOrEmpty(language))
                language = CultureInfo.CurrentCulture.Name;

            CalendarMonth[] months = new CalendarMonth[monthsCount];
            for (int xm = 0; xm < monthsCount; xm++) {
                DateTime month = DateTime.Now.AddMonths(xm);
                months[xm] = new CalendarMonth();
                months[xm].MonthIndex = month.Month;
                months[xm].MonthName = month.ToString(dateFormat, CultureInfo.GetCultureInfo(language));
            }
            return months;
        }

        #endregion

    }
}
